/**
 * Created by a1 on 16/9/20.
 */

var testData = '{"title":"HTML 教程","content":"在本教程中,你将学习如何使用 HTML 来创建站点。 HTML 很容易学习!你会喜欢它的! 现在开始学习 HTML!HTML 实例 学习","date":"2016-01-01","imgUrl":"http://img.xiami.net/images/collect_pic/11-15/1447524372_mtmI_4.jpg"}'
var detailData = JSON.parse(testData)

function PageDetailData() {
    var nodeContentImage
    var nodeContentSource
    var nodeContentDetail
    var nodeContentAction
    var nodeContentRecommend
    var nodeContentRecommendList
}
PageDetailData.prototype.initNode = function() {
    this.nodeContentImage = $("#dm_detail_top_img")
    this.nodeContentSource = $("#dm_detail_content_source")
    this.nodeContentDetail = $("#dm_detail_content_detail")
    this.nodeContentAction = $("#dm_detail_content_action")
    this.nodeContentRecommend = $("#dm_detail_content_recommend")
    this.nodeContentRecommendList = $("#dm_detail_content_recommend_list")
}

var pageDetailData

function initDetail() {
    pageDetailData = new PageDetailData()
    pageDetailData.initNode()
    pageDetailData.nodeContentRecommend.hide()
    display()
}

function onShowDetail() {
    pageDetailData.initNode()
}

function display() {
    if(!isEmpty(detailData[kImageUrl])) {
        pageDetailData.nodeContentImage.attr("src", detailData[kImageUrl])
    }
    pageDetailData.nodeContentSource.html("发表时间：" + detailData[kDate] + "&nbsp;&nbsp;&nbsp;&nbsp;来源：" + detailData["source"])
    pageDetailData.nodeContentDetail.html(detailData[kContent])
    displayActions()
    displayRecommend()
}


function displayActions() {
    var data = {"fundName":"银华货币A","info":"银华货币A,天天有收益","actions":[{"type":"1","name":"申购"},{"type":"2","name":"定投"}]}
    for(var i = 0; i < 3; ++i) {
        addListActionItem(pageDetailData.nodeContentAction, data)
    }
}

function addListActionItem(parent, data) {
    var item = $("<table class='dm_item_action'></table>")
    var content = $("<tr></tr>")
    var containerLeft = $("<td class='dm_item_action_container'></td>")
    content.append(containerLeft)
    var labelTitle = $("<div class='dm_item_action_name'>"+data["fundName"]+"</div>")
    containerLeft.append(labelTitle)
    var labelContent = $("<div class='dm_item_action_info'>"+data["info"]+"</div>")
    containerLeft.append(labelContent)

    var containerRight = $("<td class='dm_item_action_container'></td>")
    content.append(containerRight)
    var actions = data["actions"]
    for(var i = 0; i < actions.length; ++i) {
        var button = $("<a class='dm_item_action_button'>"+actions[i]["name"]+"</a>")
        containerRight.append(button)
    }
    item.append(content)
    parent.append(item)
}

function displayRecommend() {
    pageDetailData.nodeContentRecommend.show()
    for(var i = 0; i < 3; ++i) {
        detailData[kID] = "" + i
        console.log("add id = " + detailData[kID])
        var labelDate = addListItem(pageDetailData.nodeContentRecommendList, detailData, "2")
        labelDate.html("阅读:"+18)
    }
}
